package com.rd.student.ruanjian.class3.gaochenxi.day0801;

import java.util.Arrays;

class ChooseSort {
   public static void Original(int[] arr) {
      //冒泡排序
      for (int i = 0; i < arr.length - 1; i++) {
         for (int j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
            }
         }
      }
      System.out.println(Arrays.toString(arr));
   }

   //可以接收
   public static void Func(int[] arr) {
      //冒泡排序优化1
      boolean flag = true;
      for (int i = 0; i < arr.length - 1; i++) {
         for (int j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
               flag = false;
            }
         }
         if (flag) {
            break;
         }
      }
      System.out.println(Arrays.toString(arr));
   }

   //可以接收点
   public static void Func2(int[] arr) {
      //冒泡排序优化2
      boolean flag = true;
      //（1）添加changeIndex,lastChangeIndex分别为0,arr.length-1
      //（2）j<lastChangeIndex
      //（3）将changeIndex的值赋给lastChangeIndex(在if(flag)之前写)
      int changeIndex = 0;
      int lastChangeIndex = arr.length - 1;
      for (int i = 0; i < arr.length - 1; i++) {
         for (int j = 0; j < lastChangeIndex; j++) {
            if (arr[j] > arr[j + 1]) {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
               flag = false;
               changeIndex = j;
            }
         }
         lastChangeIndex = changeIndex;
         if (flag) {
            break;
         }
      }
      System.out.println(Arrays.toString(arr));
   }


   //选择排序:myself
   public static void returnArr(int[] arr) {
      for (int i = 0; i < arr.length - 1; i++) {
         for (int j = arr.length - 1; j > i; j--) {
            //一轮排序结束
            if (arr[i] > arr[j]) {
               int temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
            }
         }
      }
      System.out.println(Arrays.toString(arr));
   }


   //插入排序：myself
   public static void chaZhi(int[] arr) {
      for (int i = 0; i < arr.length; i++) {
         for (int j = i; j > 0; j--) {//这个牌和之前所有的牌进行比较
            if (arr[j] > arr[j - 1]) {//如果当前的牌比前一个牌大
               break;//就不管了
            }
            //否则的话，就把当前的牌和前一个牌互换位置
            int temp = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = temp;
         }
      }
      System.out.println(Arrays.toString(arr));
   }
}


